// pages/tag/tag.js
var app = getApp()

Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		userTagList: [],
		tagList: [],
		tagIdList: [],
		subTagList: []
	},

	// 设置用户标签
	setUserTag: function (e) {
		var that = this
		console.log(e.currentTarget.dataset.tagmsg)
		console.log(e.currentTarget.dataset.index)
		var index = e.currentTarget.dataset.index
		that.data.subTagList[index].loading = true
		that.setData({
			subTagList: that.data.subTagList
		})
		wx.request({
			url: app.globalData.baseUrl + '/api/user/settags',
			method: 'POST',
			data: {
				appid: that.data.dataInfo.user_info.appid,
				openid: 'oo5pZ0-cIg9kiDc3H0HCV9ShrXAo',
				tagid: e.currentTarget.dataset.tagmsg.group_id
			},
			success: function (res) {
				if (res.data.status === 1) {
					that.data.subTagList[index].loading = false
					that.data.subTagList[index].isHasChecked = true
					var tagIdList = []
					for (var i in res.data.tagid_list) {
						tagIdList.push(res.data.tagid_list[i])
					}
					var userTagList = []

					tagIdList.forEach(function (item) {
						for (var i = 0; i < that.data.subTagList.length; i++) {
							console.log(that.data.subTagList[i])
							if (item == that.data.subTagList[i].group_id) {
								that.data.subTagList[i].isHasChecked = true
								userTagList.push(that.data.subTagList[i])
							}
						}
					})
					that.setData({
						subTagList: that.data.subTagList,
						userTagList: userTagList
					})
				} else {
					wx.showToast({
						title: '编辑失败',
						icon: 'none',
						duration: 1000
					})
					that.data.subTagList[index].loading = false
					that.setData({
						subTagList: that.data.subTagList
					})
				}
			}
		})
	},

	// 删除用户标签
	deleteUserTag: function (e) {
		var that = this
		console.log(e.currentTarget.dataset.tagmsg)
		console.log(e.currentTarget.dataset.index)
		var index = e.currentTarget.dataset.index
		that.data.userTagList[index].loading = true
		that.setData({
			userTagList: that.data.userTagList
		})
		wx.request({
			url: app.globalData.baseUrl + '/api/user/deltags',
			method: 'POST',
			data: {
				appid: that.data.dataInfo.user_info.appid,
				openid: 'oo5pZ0-cIg9kiDc3H0HCV9ShrXAo',
				tagid: e.currentTarget.dataset.tagmsg.group_id
			},
			success: function (res) {
				console.log(res)
				if (res.data.status === 1) {
					console.log(that.data.subTagList)
					that.data.subTagList.forEach(function (item) {
						if (item.group_id == e.currentTarget.dataset.tagmsg.group_id) {
							item.isHasChecked = false
						}
					})
					var tagIdList = []
					for (var i in res.data.tagid_list) {
						tagIdList.push(res.data.tagid_list[i])
					}
					var userTagList = []
					tagIdList.forEach(function (item) {
						for (var i = 0; i < that.data.subTagList.length; i++) {
							console.log(that.data.subTagList[i])
							if (item === that.data.subTagList[i].group_id) {
								that.data.subTagList[i].isHasChecked = true
								userTagList.push(that.data.subTagList[i])
							}
						}
					})
					that.setData({
						tagIdList: tagIdList,
						subTagList: that.data.subTagList,
						userTagList: userTagList
					})
				}
			}
		})
	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function (options) {
		console.log(JSON.parse(options.userMsg))
		var data = JSON.parse(options.userMsg)
		console.log(data)
		var tagIdList = []
		for (var i in data.tagid_list) {
			tagIdList.push(data.tagid_list[i])
		}
		console.log(tagIdList)
		this.setData({
			tagIdList: tagIdList
		})
		var that = this
		wx.setNavigationBarTitle({
			title: '用户标签'
		})

		// 获取缓存的客服数据
		try {
			var value = wx.getStorageSync('islogin')
			if (value) {
				console.log(value)
				// 如果缓存里面有登录信息则发送请求
				that.setData({
					dataInfo: value
				})
			} else {
				// wx.navigateTo({
				// 	url: '../login/login'
				// })
			}
		} catch (e) {
			console.log(e)
		}
		

		// 获取公众号的标签
		wx.request({
			url: app.globalData.baseUrl + '/api/label/getlabel	',
			method: 'GET',
			data: {
				auid: that.data.dataInfo.user_info.auid
			},
			success: function (res) {
				console.log(res)
				if (res.statusCode === 200) {
					// 保存下所有的子标签
					var subTagList = []
					res.data.forEach(function (item) {
						if (item.s_label.length !== 0) {
							item.s_label.forEach(function (innerItem) {
								if (!innerItem.color) {
									innerItem.color = '#ff0000'
								}
								// 给所有的标签加一个是否当前用户已经拥有的属性  默认值是没有
								innerItem.isHasChecked = false
								innerItem.loading = false
								subTagList.push(innerItem)
							})
						}
					})
					console.log(subTagList)
					that.setData({
						tagList: res.data,
						subTagList: subTagList
					})
					// 根据标签的id拿到对应id的数据
					var userTagList = []
					console.log(that.data.tagIdList)
					that.data.tagIdList.forEach(function (item) {
						for (var i = 0; i < that.data.subTagList.length; i++) {
							if (item == that.data.subTagList[i].group_id) {
								that.data.subTagList[i].isHasChecked = true
								userTagList.push(that.data.subTagList[i])
							}
						}
					})
					// 保存当前用户所拥有的标签
					that.setData({
						userTagList: userTagList,
						subTagList: that.data.subTagList
					})
					console.log(that.data.userTagList)
				}
			}
		})
	},

	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function () {

	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function () {

	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function () {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function () {

	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh: function () {

	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function () {

	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage: function () {

	}
})